package com.github.maojx0630.easy.translate.select.impl;

import com.github.maojx0630.easy.translate.conf.TranslateConfig;
import com.github.maojx0630.easy.translate.select.DictSelect;
import com.github.maojx0630.easy.translate.select.TableSelect;
import lombok.AllArgsConstructor;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

/**
 * @author 毛家兴
 * @since 2024-02-06 09:44
 */
@AllArgsConstructor
@ConditionalOnBean(SqlSession.class)
public class SelectConfiguration implements InitializingBean {

  private SqlSession sqlSession;

  @Bean
  @ConditionalOnMissingBean(DictSelect.class)
  public DictSelect dictSelect( TranslateConfig config) {
    SelectMapper mapper = sqlSession.getConfiguration().getMapper(SelectMapper.class, sqlSession);
    return new DictSelectService(mapper,config);
  }

  @Bean
  @ConditionalOnMissingBean(TableSelect.class)
  public TableSelect tableSelect() {
    SelectMapper mapper = sqlSession.getConfiguration().getMapper(SelectMapper.class, sqlSession);
    return new TableSelectService(mapper);
  }

  @Override
  public void afterPropertiesSet() {
    sqlSession.getConfiguration().addMapper(SelectMapper.class);
  }
}
